calculator.js ➔ f_calc   F
last analyzed

Complexity

Conditions 14
Paths 22

Size

Total Lines 73
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 51
nc 22
nop 2
dl 0
loc 73
rs 3.6
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like calculator.js ➔ f_calc often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
calc_array = new Array();
0 ignored issues
show
Bug introduced by
The variable calc_array seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.calc_array.
Loading history...
Coding Style Best Practice introduced by
Using the Array constructor is generally discouraged. Consider using an array literal instead.
Loading history...
2
var calcul=0;
3
var pas_ch=0;
4
function $id(id)
5
{
6
        return document.getElementById(id);
7
}
8
function f_calc(id,n)
9
{
10
        if(n=='ce')
11
        {
12
                init_calc(id);
13
        }
14
        else if(n=='=')
15
        {
16
                if(calc_array[id][0]!='=' && calc_array[id][1]!=1)
17
                {
18
                        eval('calcul='+calc_array[id][2]+calc_array[id][0]+calc_array[id][3]+';');
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
19
                        calc_array[id][0] = '=';
20
                        $id(id+'_result').value=calcul;
21
                        calc_array[id][2]=calcul;
22
                        calc_array[id][3]=0;
23
                }
24
        }
25
        else if(n=='+-')
26
        {
27
                $id(id+'_result').value=$id(id+'_result').value*(-1);
28
                if(calc_array[id][0]=='=')
29
                {
30
                        calc_array[id][2] = $id(id+'_result').value;
31
                        calc_array[id][3] = 0;
32
                }
33
                else
34
                {
35
                        calc_array[id][3] = $id(id+'_result').value;
36
                }
37
                pas_ch = 1;
38
        }
39
        else if(n=='nbs')
40
        {
41
                if($id(id+'_result').value<10 && $id(id+'_result').value>-10)
42
                {
43
                        $id(id+'_result').value='';
44
                }
45
                else
46
                {
47
                        $id(id+'_result').value=$id(id+'_result').value.slice(0,$id(id+'_result').value.length-1);
48
                }
49
                if(calc_array[id][0]=='=')
50
                {
51
                        calc_array[id][2] = $id(id+'_result').value;
52
                        calc_array[id][3] = 0;
53
                }
54
                else
55
                {
56
                        calc_array[id][3] = $id(id+'_result').value;
57
                }
58
        }
59
        else
60
        {
61
                        if(calc_array[id][0]!='=' && calc_array[id][1]!=1)
62
                        {
63
                                eval('calcul='+calc_array[id][2]+calc_array[id][0]+calc_array[id][3]+';');
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
64
                                $id(id+'_result').value=calcul;
65
                                calc_array[id][2]=calcul;
66
                                calc_array[id][3]=0;
67
                        }
68
                        calc_array[id][0] = n;
69
        }
70
        if(pas_ch==0)
71
        {
72
                calc_array[id][1] = 1;
73
        }
74
        else
75
        {
76
                pas_ch=0;
77
        }
78
        document.getElementById(id+'_result').focus();
79
        return true;
80
}
81
function add_calc(id,n)
82
{
83
        if(calc_array[id][1]==1)
84
        {
85
                $id(id+'_result').value=n;
86
        }
87
        else
88
        {
89
                $id(id+'_result').value+=n;
90
        }
91
        if(calc_array[id][0]=='=')
92
        {
93
                calc_array[id][2] = $id(id+'_result').value;
94
                calc_array[id][3] = 0;
95
        }
96
        else
97
        {
98
                calc_array[id][3] = $id(id+'_result').value;
99
        }
100
        calc_array[id][1] = 0;
101
        document.getElementById(id+'_result').focus();
102
        return true;
103
}
104
function init_calc(id)
105
{
106
        $id(id+'_result').value='';
107
        calc_array[id] = new Array('=',1,'0','0',0);
0 ignored issues
show
Coding Style Best Practice introduced by
Using the Array constructor is generally discouraged. Consider using an array literal instead.
Loading history...
108
        document.getElementById(id+'_result').focus();
109
        return true;
110
}
111
function key_detect_calc(id,evt)
112
{
113
        if((evt.keyCode>95) && (evt.keyCode<106))
114
        {
115
                var nbr = evt.keyCode-96;
116
                add_calc(id,nbr);
117
        }
118
        else if((evt.keyCode>47) && (evt.keyCode<58))
119
        {
120
                var nbr = evt.keyCode-48;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nbr already seems to be declared on line 115. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
121
                add_calc(id,nbr);
122
        }
123
        else if(evt.keyCode==107)
124
        {
125
                f_calc(id,'+');
126
        }
127
        else if(evt.keyCode==109)
128
        {
129
                f_calc(id,'-');
130
        }
131
        else if(evt.keyCode==106)
132
        {
133
                f_calc(id,'*');
134
        }
135
        else if(evt.keyCode==111)
136
        {
137
                f_calc(id,'');
138
        }
139
        else if(evt.keyCode==110)
140
        {
141
                add_calc(id,'.');
142
        }
143
        else if(evt.keyCode==190)
144
        {
145
                add_calc(id,'.');
146
        }
147
        else if(evt.keyCode==188)
148
        {
149
                add_calc(id,'.');
150
        }
151
        else if(evt.keyCode==13)
152
        {
153
                f_calc(id,'=');
154
        }
155
        else if(evt.keyCode==46)
156
        {
157
                f_calc(id,'ce');
158
        }
159
        else if(evt.keyCode==8)
160
        {
161
                f_calc(id,'nbs');
162
        }
163
        else if(evt.keyCode==27)
164
        {
165
                f_calc(id,'ce');
166
        }
167
        return true;
168
}